package com.css.zfzx.fwsjcj.modules.taskmanage.taskfrom.service.Impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import cn.hutool.core.util.ReflectUtil;
import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.org.dept.repository.entity.DeptEntity;
import com.css.bpm.platform.org.division.repository.DivisionRepository;
import com.css.bpm.platform.org.division.repository.entity.DivisionEntity;
import com.css.bpm.platform.org.role.repository.entity.RoleEntity;
import com.css.bpm.platform.org.user.repository.entity.UserEntity;
import com.css.bpm.platform.utils.PlatformPageUtils;
import com.css.bpm.platform.utils.PlatformSessionUtils;
import com.css.bpm.platform.utils.UUIDGenerator;
import com.css.zfzx.fwsjcj.commen.util.Constant;
import com.css.zfzx.fwsjcj.commen.util.ServerUtil;
import com.css.zfzx.fwsjcj.modules.analysis.vo.AnalysisQueryVo;
import com.css.zfzx.fwsjcj.modules.group.entity.FwGroupEntity;
import com.css.zfzx.fwsjcj.modules.group.repository.FwGroupRepository;
import com.css.zfzx.fwsjcj.modules.quality.vo.QualityVo;
import com.css.zfzx.fwsjcj.modules.structure.entity.*;
import com.css.zfzx.fwsjcj.modules.structure.vo.CheckVo;
import com.css.zfzx.fwsjcj.modules.taskmanage.taskfrom.entity.*;
import com.css.zfzx.fwsjcj.modules.taskmanage.taskfrom.repository.TaskFormQueryRepository;
import com.css.zfzx.fwsjcj.modules.taskmanage.taskfrom.repository.TaskFormRepository;
import com.css.zfzx.fwsjcj.modules.taskmanage.taskfrom.service.*;
import com.css.zfzx.fwsjcj.modules.taskmanage.taskfrom.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Slf4j
@Service
@PropertySource("classpath:platform-config.yml")
public class TaskFormServiceImpl implements TaskFromService {
    @Value("${image.urlPre}")
    private String urlPre;
    @Autowired
    TaskFormRepository taskFormRepository;
    @Autowired
    TaskFormQueryRepository taskFormQueryRepository;
    @Autowired
    CityTaskFormService cityTaskFormService;
    @Autowired
    NoCityTaskFromService noCityTaskFromService;
    @Autowired
    VillageHouseFormService villageHouseFormService;
    @Autowired
    VillagePubHouseFormService villagePubHouseFormService;
    @Autowired
    DivisionRepository divisionRepository;
    // 超级管理员、省、市管理员编号
    @Value("${role.superCode}")
    private String superCode;
    @Value("${role.provinceCode}")
    private String provinceCode;
    @Value("${role.cityCode}")
    private String cityCode;
    @Value("${role.areaCode}")
    private String areaCode;//县管理员编号
    @Value("${role.xcCollectCode}")
    private String xcCollectCode;//详查采集员
    @Autowired
    FwGroupRepository fwGroupRepository;
    /**
     * 图片服务器文件盘符
     **/
    @Value("${image.localDir}")
    private String localDir;

    @Override
    public void saveForm(FwFormEntity fwFormEntity) {
        fwFormEntity.setFwId(UUIDGenerator.getUUID());
        fwFormEntity.setIsValid(Constant.IS_VALID_YES);
        if(fwFormEntity.getCreateUserId()==null){
            fwFormEntity.setCreateUserId(PlatformSessionUtils.getUserId());
        }
        taskFormRepository.save(fwFormEntity);
    }

    @Override
    public JSONObject findAll(HttpServletRequest request, TaskFormQueryVO taskFormQueryVO, int curPage, int pageSize) {
        Page<FwFormEntity> page = taskFormQueryRepository.findPage(request, taskFormQueryVO, curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(page);
        return jsonObject;
    }

    @Transactional
    @Override
    public void subAll(List<String> ids, String spType) {
        taskFormRepository.subAllByIds(spType, ids, new Date());
        List<FwFormEntity> byId = findById(ids);
        for (FwFormEntity entity : byId) {
            if (Constant.BULIDING_CITY.equals(entity.getBulidingType())) {
                cityTaskFormService.subById(entity.getFormId(), spType);
            }
            if (Constant.BULIDING_NOCITY.equals(entity.getBulidingType())) {
                noCityTaskFromService.subById(entity.getFormId(), spType);
            }
            if (Constant.BULIDING_VIllAGE.equals(entity.getBulidingType())) {
                villageHouseFormService.subById(entity.getFormId(), spType);
            }
            if (Constant.BULIDING_VIllAGEPUB.equals(entity.getBulidingType())) {
                villagePubHouseFormService.subById(entity.getFormId(), spType);
            }
        }
    }

    @Transactional
    @Override
    public void deletAll(List<String> ids) {
        taskFormRepository.delByIds(ids);
        List<FwFormEntity> byId = findById(ids);
        for (FwFormEntity entity : byId) {
            if (Constant.BULIDING_CITY.equals(entity.getBulidingType())) {
                cityTaskFormService.deleteById(entity.getFormId());
            }
            if (Constant.BULIDING_NOCITY.equals(entity.getBulidingType())) {
                noCityTaskFromService.deleteById(entity.getFormId());
            }
            if (Constant.BULIDING_VIllAGE.equals(entity.getBulidingType())) {
                villageHouseFormService.deleteById(entity.getFormId());
            }
            if (Constant.BULIDING_VIllAGEPUB.equals(entity.getBulidingType())) {
                villagePubHouseFormService.deleteById(entity.getFormId());
            }
        }
    }

    @Override
    public List<FwFormEntity> findById(List<String> ids) {
        List<FwFormEntity> allById = taskFormRepository.findAllById(ids);
        return allById;
    }

    @Override
    public FwFormEntity findByFormId(String formId) {
        FwFormEntity byFormId = taskFormRepository.findByFormId(formId);
        return byFormId;
    }

    @Override
    public void updForm(FwFormEntity fwFormEntity) {
        taskFormRepository.save(fwFormEntity);
    }

    @Override
    public List<FwFormEntity> queryAll(HttpServletRequest request) {
        List<FwFormEntity> all = taskFormRepository.findAllValid();
        return all;
    }

    @Override
    public Long findByUserIdCount(String userId) {
        Long count = taskFormRepository.findByCount(userId);
        return count;
    }
    private String getRoleByUserId(String userId) {
        // 获取当前登录用户角色
        List<RoleEntity> roles = PlatformAPI.getOrgAPI().getUserAPI().getRoles(userId);
        // 角色列表里面是否包含三个管理员
        //是否为超级管理员
        if (ServerUtil.isContaintRole(roles, superCode)){
            return "1";
        }
        //是否为省级管理员
        if (ServerUtil.isContaintRole(roles, provinceCode)){
            return "2";
        }
        //是否为市级管理员
        if (ServerUtil.isContaintRole(roles, cityCode)){
            return "3";
        }
        //是否为县级管理员
        if (ServerUtil.isContaintRole(roles, areaCode)){
            return "4";
        }
        /*//是否为详查采集员
        if (ServerUtil.isContaintRole(roles, xcCollectCode)){
            return "5";
        }*/
        //是否是组长
        List<FwGroupEntity> byGroupLeaderId = fwGroupRepository.findAllByGroupLeaderId(userId);
        if (byGroupLeaderId != null && byGroupLeaderId.size() > 0){
            return "6";
        }
        return "";
    }
    @Override
    public JSONObject findPage(HttpServletRequest request, TaskFormPCQueryVo taskFormPCQueryVo, int curPage, int pageSize) {
        String userId = PlatformSessionUtils.getUserId();
        String role = getRoleByUserId(userId);
        Page<FwFormEntity> pcPage = taskFormQueryRepository.findAllPage(role,taskFormPCQueryVo, curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(pcPage);
        return jsonObject;
    }

    @Transactional
    @Override
    public void spForm(SpFormVO spFormVO) {
        String reason = spFormVO.getReason();
        String status = spFormVO.getStatus();
        String formId = spFormVO.getFormId();
       /* String[] split = formId.split(",");
        List<String> list = Arrays.asList(split);*/
        taskFormRepository.spForm(status, reason, formId, new Date());
        if (Constant.BULIDING_CITY.equals(spFormVO.getBuildingType())) {
            cityTaskFormService.spReason(status, reason, formId);
        }
        if (Constant.BULIDING_NOCITY.equals(spFormVO.getBuildingType())) {
            noCityTaskFromService.spReason(status, reason, formId);
        }
        if (Constant.BULIDING_VIllAGE.equals(spFormVO.getBuildingType())) {
            villageHouseFormService.spReason(status, reason, formId);
        }
        if (Constant.BULIDING_VIllAGEPUB.equals(spFormVO.getBuildingType())) {
            villagePubHouseFormService.spReason(status, reason, formId);
        }
    }

    @Transactional
    @Override
    public void spAllFrom(String ids, String type, String reason) {
        String[] split = ids.split(",");
        List<String> list = Arrays.asList(split);
        List<FwFormEntity> allById = taskFormRepository.findAllById(list);
        taskFormRepository.subAll(type, list);
        for (FwFormEntity fwFormEntity : allById) {
            String formId = fwFormEntity.getFormId();
            String bulidingType = fwFormEntity.getBulidingType();
            if (Constant.BULIDING_CITY.equals(bulidingType)) {
                cityTaskFormService.spReason(type, reason, formId);
            }
            if (Constant.BULIDING_NOCITY.equals(bulidingType)) {
                noCityTaskFromService.spReason(type, reason, formId);
            }
            if (Constant.BULIDING_VIllAGE.equals(bulidingType)) {
                villageHouseFormService.spReason(type, reason, formId);
            }
            if (Constant.BULIDING_VIllAGEPUB.equals(bulidingType)) {
                villagePubHouseFormService.spReason(type, reason, formId);
            }
        }
    }

    @Override
    public List<FwFormEntity> findByAddress(String province, String city,String area) {
        StringBuilder sb = new StringBuilder();
        if (!StringUtils.isEmpty(province)) {
            sb.append(province);
            if (!StringUtils.isEmpty(city)) {
                sb.append("," + city);
            }
            if (!StringUtils.isEmpty(area)) {
                sb.append("," + area);
            }
            String add = sb.toString();
            List<FwFormEntity> taskFormLike = taskFormRepository.findLike(add);
            return taskFormLike;
        } else {
            return null;
        }

    }

    @Override
    public List<FwFormEntity> findAllCountry() {
        List<FwFormEntity> taskFormLike = taskFormRepository.findAllCountry();
        return taskFormLike;
    }

    @Override
    public JSONObject findPage(QualityVo qualityVo, int curPage, int pageSize) {
        Page<FwFormEntity> page = taskFormQueryRepository.findPage(qualityVo, curPage, pageSize);
        return PlatformPageUtils.formatPageData(page);
    }

    @Override
    public List<FwFormEntity> findByTaskId(String id) {
        Specification<FwFormEntity> specification = new Specification<FwFormEntity>() {
            @Override
            public Predicate toPredicate(Root<FwFormEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                predicates.add(criteriaBuilder.equal(root.get("taskId"), id));
                // 有效标识，即为有效的
                predicates.add(criteriaBuilder.equal(root.get("isValid"), Constant.IS_VALID_YES));
                Predicate predicate = criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
                return predicate;
            }
        };
        return taskFormRepository.findAll(specification);
    }

    @Override
    public List<FwFormEntity> buildingByBuilding(String buildingName) {
        List<FwFormEntity> likeBuidingName = taskFormRepository.findLikeBuidingName(buildingName);
        return likeBuidingName;
    }

    @Override
    public void export(TaskFormPCQueryVo taskFormPCQueryVo, HttpServletResponse response) {
        // 根据普查表类型查出子表id集合
        List<String> formIdList = taskFormQueryRepository.findFormIdListBybuildingType(taskFormPCQueryVo);
        if (formIdList == null || formIdList.size() <= 0) {
            throw new RuntimeException("类型为" + castType(taskFormPCQueryVo.getBuildingType()) + "的数据为空！");
        }
        // 根据普查表类型导出
        if (Constant.BULIDING_CITY.equals(taskFormPCQueryVo.getBuildingType())) {
            cityTaskFormService.export(formIdList, response);
        }
        if (Constant.BULIDING_NOCITY.equals(taskFormPCQueryVo.getBuildingType())) {
            noCityTaskFromService.export(formIdList, response);
        }
        if (Constant.BULIDING_VIllAGE.equals(taskFormPCQueryVo.getBuildingType())) {
            villageHouseFormService.export(formIdList, response);
        }
        if (Constant.BULIDING_VIllAGEPUB.equals(taskFormPCQueryVo.getBuildingType())) {
            villagePubHouseFormService.export(formIdList, response);
        }
    }

    @Override
    public Boolean isEmpty(TaskFormPCQueryVo taskFormPCQueryVo) {
        List<String> list = taskFormQueryRepository.findFormIdListBybuildingType(taskFormPCQueryVo);
        if (list == null || list.size() == 0) {
            return true;
        }
        return false;
    }

    @Override
    public GeneralVo generalTitle() {
        Calendar now = Calendar.getInstance();
        // 创建VO,默认传值年月日
        GeneralVo vo = new GeneralVo(now.get(Calendar.YEAR), now.get(Calendar.MONTH) + 1, now.get(Calendar.DAY_OF_MONTH));
        // 城镇住宅
        long cityTotalNum = cityTaskFormService.count(null);
        int cityTotalArea = cityTaskFormService.areaCount(null);
        vo.setCityTotalNum(cityTotalNum);
        vo.setCityTotalArea(cityTotalArea);
        // 城镇非住宅
        long noCityTotalNum = noCityTaskFromService.count(null);
        int noCityTotalArea = noCityTaskFromService.areaCount(null);
        vo.setNoCityTotalNum(noCityTotalNum);
        vo.setNoCityTotalArea(noCityTotalArea);
        // 农村住房
        long villageTotalNum = villageHouseFormService.count(null);
        int villageTotalArea = villageHouseFormService.areaCount(null);
        vo.setVillageTotalNum(villageTotalNum);
        vo.setVillageTotalArea(villageTotalArea);
        // 农村公共建筑
        long villagePubTotalNum = villagePubHouseFormService.count(null);
        int villagePubTotalArea = villagePubHouseFormService.areaCount(null);
        vo.setVillagePubTotalNum(villagePubTotalNum);
        vo.setVillagePubTotalArea(villagePubTotalArea);
        // 建筑总栋数、总面积
        long totalNum = cityTotalNum + noCityTotalNum + villageTotalNum + villagePubTotalNum;
        int totalArea = cityTotalArea + noCityTotalArea + villageTotalArea + villagePubTotalArea;
        vo.setTotalNum(totalNum);
        vo.setTotalArea(totalArea);
        // 获取省、市去重数量
        List<String> province = taskFormRepository.listProvince();
        List<String> city = taskFormRepository.listCity();
        vo.setProviceNum(province.size());
        vo.setCityNum(city.size());
        return vo;
    }

    @Override
    public List<GeneralVo> listGeneral(GeneralQueryVo generalQueryVo) {
        // 31个省
        List<DivisionEntity> subDivisions = PlatformAPI.getDivisionAPI().getSubDivisions("root");
        ArrayList<GeneralVo> generalVos = new ArrayList<>();
        for (DivisionEntity divisionEntity : subDivisions) {
            GeneralVo generalVo = new GeneralVo();
            // 统计分析查询vo
            AnalysisQueryVo vo = new AnalysisQueryVo();
            vo.setBelongtoProvince(divisionEntity.getDivisionName());
            if (generalQueryVo != null) {
                // 查询时间段
                try {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月份是MM
                    String beginDate = generalQueryVo.getBeginDate();
                    String endDate = generalQueryVo.getEndDate();
                    if (!StringUtils.isEmpty(beginDate)) {
                        beginDate += " 00:00:00";
                        vo.setBeginDate(sdf.parse(beginDate));
                    }
                    if (!StringUtils.isEmpty(endDate)) {
                        endDate += " 23:59:59";
                        vo.setEndDate(sdf.parse(endDate));
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                // 查询审核状态
                vo.setSpType(generalQueryVo.getStatus());
            }
            // 省
            generalVo.setProvice(divisionEntity.getDivisionName());
            // 栋数
            long cityNum = cityTaskFormService.count(vo);
            long noCityNum = noCityTaskFromService.count(vo);
            long villageNum = villageHouseFormService.count(vo);
            long villagePubNum = villagePubHouseFormService.count(vo);
            long totalNum = cityNum + noCityNum + villageNum + villagePubNum;
            generalVo.setNum(cityNum, noCityNum, villageNum, villagePubNum, totalNum);
            // 面积
            int cityArea = cityTaskFormService.areaCount(vo);
            int noCityArea = noCityTaskFromService.areaCount(vo);
            int villageArea = villageHouseFormService.areaCount(vo);
            int villagePubArea = villagePubHouseFormService.areaCount(vo);
            int totalArea = cityArea + noCityArea + villageArea + villagePubArea;
            generalVo.setArea(cityArea, noCityArea, villageArea, villagePubArea, totalArea);

            generalVos.add(generalVo);
        }
        return generalVos;
    }

    @Override
    public List<GeneralVo> listCityGeneral(GeneralQueryVo generalQueryVo) {
        // 省或者直辖市名称
        String provice = generalQueryVo.getDivisionName();
        // 省或者直辖市ID
        String divisionId = generalQueryVo.getDivisionId();
        // 省、直辖市的名称和ID不能为空！
        if (StringUtils.isEmpty(provice) || StringUtils.isEmpty(divisionId)) {
            throw new RuntimeException("省、直辖市的名称和ID不能为空！");
        }
        // 获取省下面的市或者直辖市下面的区
        List<DivisionEntity> subDivisions = PlatformAPI.getDivisionAPI().getSubDivisions(divisionId);
        ArrayList<GeneralVo> generalVos = new ArrayList<>();
        for (DivisionEntity divisionEntity : subDivisions) {
            // 单个结果
            GeneralVo generalVo = new GeneralVo();
            // 统计分析查询vo
            AnalysisQueryVo vo = new AnalysisQueryVo();
            // 省
            vo.setBelongtoProvince(provice);
            if (provice.equals("北京市") || provice.equals("天津市") ||
                    provice.equals("上海市") || provice.equals("重庆市")) {
                // 若是直辖市则查询区
                vo.setBelongtoArea(divisionEntity.getDivisionName());
            } else {
                // 非直辖市查询市
                vo.setBelongtoCity(divisionEntity.getDivisionName());
            }
            if (generalQueryVo != null) {
                try {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月份是MM
                    String beginDate = generalQueryVo.getBeginDate();
                    String endDate = generalQueryVo.getEndDate();
                    if (!StringUtils.isEmpty(beginDate)) {
                        beginDate += " 00:00:00";
                        vo.setBeginDate(sdf.parse(beginDate));
                    }
                    if (!StringUtils.isEmpty(endDate)) {
                        endDate += " 23:59:59";
                        vo.setEndDate(sdf.parse(endDate));
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                // 查询审核状态
                vo.setSpType(generalQueryVo.getStatus());
            }
            // 省、市
            generalVo.setProvice(provice);
            generalVo.setCity(divisionEntity.getDivisionName());
            // 栋数
            long cityNum = cityTaskFormService.count(vo);
            long noCityNum = noCityTaskFromService.count(vo);
            long villageNum = villageHouseFormService.count(vo);
            long villagePubNum = villagePubHouseFormService.count(vo);
            long totalNum = cityNum + noCityNum + villageNum + villagePubNum;
            generalVo.setNum(cityNum, noCityNum, villageNum, villagePubNum, totalNum);
            // 面积
            int cityArea = cityTaskFormService.areaCount(vo);
            int noCityArea = noCityTaskFromService.areaCount(vo);
            int villageArea = villageHouseFormService.areaCount(vo);
            int villagePubArea = villagePubHouseFormService.areaCount(vo);
            int totalArea = cityArea + noCityArea + villageArea + villagePubArea;
            generalVo.setArea(cityArea, noCityArea, villageArea, villagePubArea, totalArea);
            generalVos.add(generalVo);
        }
        return generalVos;
    }

    @Override
    public JSONObject proviceChart(GeneralQueryVo generalQueryVo) {
        JSONObject result = new JSONObject();
        List<GeneralVo> generalVos = this.listGeneral(generalQueryVo);
        // 省份数组
        ArrayList<String> provices = new ArrayList<>();
        // 栋数数组
        ArrayList<Long> cityNum = new ArrayList<>();
        ArrayList<Long> noCityNum = new ArrayList<>();
        ArrayList<Long> villageNum = new ArrayList<>();
        ArrayList<Long> villagePubNum = new ArrayList<>();
        // 面积
        ArrayList<Long> cityArea = new ArrayList<>();
        ArrayList<Long> noCityArea = new ArrayList<>();
        ArrayList<Long> villageArea = new ArrayList<>();
        ArrayList<Long> villagePubArea = new ArrayList<>();
        for (GeneralVo generalVo : generalVos) {
            provices.add(generalVo.getProvice());
            // 栋数
            cityNum.add(generalVo.getCityTotalNum());
            noCityNum.add(generalVo.getNoCityTotalNum());
            villageNum.add(generalVo.getVillageTotalNum());
            villagePubNum.add(generalVo.getVillagePubTotalNum());
            // 面积
            cityArea.add(generalVo.getCityTotalArea());
            noCityArea.add(generalVo.getNoCityTotalArea());
            villageArea.add(generalVo.getVillageTotalArea());
            villagePubArea.add(generalVo.getVillagePubTotalArea());
        }
        // 省
        result.put("provices", provices);
        // 栋数
        result.put("cityNum", cityNum);
        result.put("noCityNum", noCityNum);
        result.put("villageNum", villageNum);
        result.put("villagePubNum", villagePubNum);
        // 面积
        result.put("cityArea", cityArea);
        result.put("noCityArea", noCityArea);
        result.put("villageArea", villageArea);
        result.put("villagePubArea", villagePubArea);

        return result;
    }

    @Override
    public JSONObject cityChart(GeneralQueryVo generalQueryVo) {
        JSONObject result = new JSONObject();
        List<GeneralVo> generalVos = this.listCityGeneral(generalQueryVo);
        // 市数组
        ArrayList<String> cities = new ArrayList<>();
        // 栋数数组
        ArrayList<Long> cityNum = new ArrayList<>();
        ArrayList<Long> noCityNum = new ArrayList<>();
        ArrayList<Long> villageNum = new ArrayList<>();
        ArrayList<Long> villagePubNum = new ArrayList<>();
        // 面积
        ArrayList<Long> cityArea = new ArrayList<>();
        ArrayList<Long> noCityArea = new ArrayList<>();
        ArrayList<Long> villageArea = new ArrayList<>();
        ArrayList<Long> villagePubArea = new ArrayList<>();
        for (GeneralVo generalVo : generalVos) {
            cities.add(generalVo.getCity());
            // 栋数
            cityNum.add(generalVo.getCityTotalNum());
            noCityNum.add(generalVo.getNoCityTotalNum());
            villageNum.add(generalVo.getVillageTotalNum());
            villagePubNum.add(generalVo.getVillagePubTotalNum());
            // 面积
            cityArea.add(generalVo.getCityTotalArea());
            noCityArea.add(generalVo.getNoCityTotalArea());
            villageArea.add(generalVo.getVillageTotalArea());
            villagePubArea.add(generalVo.getVillagePubTotalArea());
        }
        // 市
        result.put("cities", cities);
        // 栋数
        result.put("cityNum", cityNum);
        result.put("noCityNum", noCityNum);
        result.put("villageNum", villageNum);
        result.put("villagePubNum", villagePubNum);
        // 面积
        result.put("cityArea", cityArea);
        result.put("noCityArea", noCityArea);
        result.put("villageArea", villageArea);
        result.put("villagePubArea", villagePubArea);

        return result;
    }

    @Override
    public JSONObject userDivision() {
        JSONObject division = new JSONObject();

        String userId = PlatformSessionUtils.getUserId();
        List<RoleEntity> roles = PlatformAPI.getOrgAPI().getUserAPI().getRoles(userId);
        // 是否为超级管理员
        boolean containtRole = ServerUtil.isContaintRole(roles, superCode);
        DeptEntity mainDept = PlatformAPI.getOrgAPI().getUserAPI().getMainDept(userId);
        if ("root".equals(mainDept.getDeptId()) || containtRole) {
            division.put("message", "超级管理员");
        } else {
            division.put("message", "非超级管理员");
            String divisionId = mainDept.getDivisionId();
            DivisionEntity divisionEntity = divisionRepository.findValidDivisionById(divisionId);
            String divisionName = divisionEntity.getDivisionName();
            // 省
            if (divisionName.contains("省") || divisionName.contains("自治区") || divisionName.equals("北京市")
                    || divisionName.equals("天津市") || divisionName.equals("上海市") || divisionName.equals("重庆市")) {
                division.put("divisionId", divisionId);
                division.put("divisionName", divisionName);
            } else { // 区，获取父
                String parentId = divisionEntity.getParentId();
                DivisionEntity divisionParentEntity = divisionRepository.findValidDivisionById(parentId);
                String divisionParentName = divisionParentEntity.getDivisionName();
                // 父的地区名称和ID
                division.put("divisionId", parentId);
                division.put("divisionName", divisionParentName);
                // 地区名称和ID
                division.put("cityDivisionId", divisionId);
                division.put("cityDivisionName", divisionName);
            }
        }
        return division;
    }

    @Override
    public List<String> listFormByType(String type, String startDate, String endDate, String province) {
        // 时间段
        Date start = null;
        Date end = null;
        if (!StringUtils.isEmpty(startDate) || !StringUtils.isEmpty(endDate)) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                // 结束时间为空
                if (StringUtils.isEmpty(endDate)) {
                    startDate += " 00:00:00";
                    start = sdf.parse(startDate);
                }
                // 开始时间为空
                else if (StringUtils.isEmpty(startDate)) {
                    endDate += " 23:59:59";
                    end = sdf.parse(endDate);
                }
                // 都不为空
                else {
                    startDate += " 00:00:00";
                    start = sdf.parse(startDate);
                    endDate += " 23:59:59";
                    end = sdf.parse(endDate);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return taskFormRepository.findFormIdListBybuildingType(type, start, end, province);
    }

    @Override
    public List<String> listImagesByType(String type, String startDate, String endDate, String province) {
        ArrayList<String> imageList = new ArrayList<>();
        // 先根据条件从主表获取子表id集合
        List<String> idList = listFormByType(type, startDate, endDate, province);
        // 城镇住宅
        if (Constant.BULIDING_CITY.equals(type)) {
            List<FwCtiyHouseEntity> ctiyHouseEntities = cityTaskFormService.findImageURL(idList);
            for (FwCtiyHouseEntity cityEntity : ctiyHouseEntities) {
                String images = cityEntity.getImage();
                if (StringUtils.isNotBlank(images)) {
                    splitImage(images, imageList);
                }
            }
        }
        // 城镇非住宅
        if (Constant.BULIDING_NOCITY.equals(type)) {
            List<FwNoctiyHouseEntity> noctiyHouseEntityList = noCityTaskFromService.findImageURL(idList);
            for (FwNoctiyHouseEntity cityEntity : noctiyHouseEntityList) {
                String images = cityEntity.getImage();
                if (StringUtils.isNotBlank(images)) {
                    splitImage(images, imageList);
                }
            }
        }
        // 农村住房
        if (Constant.BULIDING_VIllAGE.equals(type)) {
            List<FwVillageHouseEntity> villageHouseEntityList = villageHouseFormService.findImageURL(idList);
            for (FwVillageHouseEntity cityEntity : villageHouseEntityList) {
                String images = cityEntity.getImage();
                if (StringUtils.isNotBlank(images)) {
                    splitImage(images, imageList);
                }
            }
        }
        // 农村公共建筑
        if (Constant.BULIDING_VIllAGEPUB.equals(type)) {
            List<FwVillagePubhouseEntity> villagePubhouseEntityList = villagePubHouseFormService.findImageURL(idList);
            for (FwVillagePubhouseEntity cityEntity : villagePubhouseEntityList) {
                String images = cityEntity.getImage();
                if (StringUtils.isNotBlank(images)) {
                    splitImage(images, imageList);
                }
            }
        }
        return imageList;
    }

    @Override
    public String importTask(MultipartFile file, String category, String taskName, String taskId) {
        try {
            // 返回的消息
            StringBuilder returnMsg = new StringBuilder();
            // 导入的参数信息
            ImportParams params = new ImportParams();
            // 设置简析的第一行
            params.setTitleRows(0);
            params.setHeadRows(1);
            // 是否需要校验
            params.setNeedVerify(true);
            // 获取到Excel数据
            if (Constant.BULIDING_CITY.equals(category)) {
                String cityTaskMsg=cityTaskFormService.importCityTask(file,params,taskName,taskId);
                returnMsg.append(cityTaskMsg);
            }
            if (Constant.BULIDING_NOCITY.equals(category)) {
                String noCityTaskMsg=noCityTaskFromService.importCityTask(file,params,taskName,taskId);
                returnMsg.append(noCityTaskMsg);
            }
            if (Constant.BULIDING_VIllAGE.equals(category)) {
                String villageTaskMsg=villageHouseFormService.importCityTask(file,params,taskName,taskId);
                returnMsg.append(villageTaskMsg);
            }
            if (Constant.BULIDING_VIllAGEPUB.equals(category)) {
                String villagePubTaskMsg=villagePubHouseFormService.importCityTask(file,params,taskName,taskId);
                returnMsg.append(villagePubTaskMsg);
            }


            return returnMsg.toString();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return "导入失败，请检查数据正确性";
        }
    }

    //获取区号
    @Override
    public String getDivisionCode(String buildingArea) {
        String divisionCode=taskFormRepository.getDivisionCode(buildingArea);
        return divisionCode;
    }

    //根据编号获取用户id
    @Override
    public Map<String,Object> getUserInfo(String userCode) {
        Map<String,Object> userInfo=taskFormRepository.getUserInfo(userCode);
        return userInfo;
    }

    /**
     * 对图片URL进行切割后放入list
     *
     * @param images
     * @param imageList
     */
    private void splitImage(String images, ArrayList<String> imageList) {
        if (StringUtils.isNotBlank(images)) {
            String[] imageSplit = images.split(",");
            for (String str : imageSplit) {
                /*int i = str.lastIndexOf("/");
                // 不包含/,如: "1.pnag"
                String substr = str.substring(i + 1);
                imageList.add(localDir + substr);*/
                imageList.add(str);
            }
        }
    }

    /**
     * 数字值转换为对应汉字
     *
     * @param type
     * @return
     */
    private String castType(String type) {
        if (Constant.BULIDING_CITY.equals(type)) {
            type = "城镇住宅";
        }
        if (Constant.BULIDING_NOCITY.equals(type)) {
            type = "城镇非住宅";
        }
        if (Constant.BULIDING_VIllAGE.equals(type)) {
            type = "农村住房";
        }
        if (Constant.BULIDING_VIllAGEPUB.equals(type)) {
            type = "农村公共建筑";
        }
        return type;
    }

    @Override
    public List<String> uploadBase64(String[] base64Info, String buildingCode) throws IOException {
        if (base64Info.length <= 0) {
            return null;
        } else {
            List<String> listPre = new ArrayList<>();
            for (int i = 0; i < base64Info.length; i++) {
                if (!org.springframework.util.StringUtils.isEmpty(base64Info[i])) {
                    if (!String.valueOf(base64Info[i]).contains("data:image")) {
                        listPre.add(String.valueOf(base64Info[i]));
                    } else {
                        //图片名称
                        String hHmmssSSS = LocalDateTime.now().format(DateTimeFormatter.ofPattern("HHmmssSSS"));
                        String realFileName = buildingCode + "_" + hHmmssSSS + "_000" + (i + 1) + ".jpeg";
                        //确保图片名称的唯一性
                        //本地存储路径
                        String dateDir = new SimpleDateFormat("yyyy/MM/dd").format(new Date());
                        String savePath = localDir + File.separator + dateDir;
                        //输出文件
                        String outFile = savePath + File.separator + realFileName;
                        //虚拟路径
                        String imagePath = urlPre + File.separator + dateDir + File.separator + realFileName;

                        BASE64Decoder decoder = new BASE64Decoder();
                        byte[] data = decoder.decodeBuffer(String.valueOf(base64Info[i]).split(",")[1]);
                        // 调整异常数据
                        for (int j = 0; j < data.length; ++j) {
                            if (data[j] < 0) {
                                data[j] += 256;
                            }
                        }
                        File file = new File(savePath);
                        if (!file.exists()) {
                            file.mkdirs();
                        }
                        OutputStream os = new FileOutputStream(outFile);
                        os.write(data);
                        os.close();
                        listPre.add(imagePath);
                    }
                }
            }
            return listPre;
        }
    }

    @Override
    public Object getChild(String id) {

        Object object = new Object();
        FwFormEntity fwFormEntity = taskFormRepository.findByFormId(id);
        //String buildingCode = fwFormEntity.getBuildingCode();
        String buildingType = fwFormEntity.getBulidingType();
        // 修改子表
        if (Constant.BULIDING_CITY.equals(buildingType)) {
            // 城镇住宅子表
            object = cityTaskFormService.findByFwId(id);
        }
        if (Constant.BULIDING_NOCITY.equals(buildingType)) {
            // 城镇非住宅子表
            object = noCityTaskFromService.findByFwId(id);
        }
        if (Constant.BULIDING_VIllAGE.equals(buildingType)) {
            // 农村住房子表
            object = villageHouseFormService.findByFwId(id);
        }
        if (Constant.BULIDING_VIllAGEPUB.equals(buildingType)) {
            //农村公共建筑
            object = villagePubHouseFormService.findByFwId(id);
        }

        return object;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void back(String id, CheckVo checkVo) {
        // 获取当前用户信息
        String userId = PlatformSessionUtils.getUserId();
        UserEntity user = PlatformAPI.getOrgAPI().getUserAPI().getUser(userId);
        FwFormEntity fwFormEntity = taskFormRepository.findByFormId(id);
        String userLevel = checkVo.getUserLevel();
        // 主表设置退回状态与原因
        fwFormEntity.setSpType(Constant.SP_NOT);
        fwFormEntity.setBackOpinion(checkVo.getBackOpinion());
//        structureFormEntity.setUpdateTime(new Date());
//        structureFormEntity.setStatus(checkVo.getStatus());
        //超级管理员
//        if("5".equals(userLevel)){
//            structureFormEntity.setSuperBackOpinion(checkVo.getSuperOpinion());
//        }else{
//            structureFormEntity.setProvinceBackOpinion(checkVo.getProvinceOpinion());
//        }
        this.updForm(fwFormEntity);


        // 根据类型判断出具体子表，根据子表Id修改子表审核信息
        String bulidingType = fwFormEntity.getBulidingType();
        // 修改子表
        if (Constant.BULIDING_CITY.equals(bulidingType)) {
            // 城镇住宅子表
            FwCtiyHouseEntity ctiyHouseEntity = cityTaskFormService.findCityById(id);
            ctiyHouseEntity.setSpType(Constant.SP_NOT);
            ctiyHouseEntity.setUpdateUser(user.getUserId());
            ctiyHouseEntity.setUpdateTime(new Date());
            ctiyHouseEntity.setBackOpinion(checkVo.getBackOpinion());
            cityTaskFormService.update(ctiyHouseEntity);
        }
        if (Constant.BULIDING_NOCITY.equals(bulidingType)) {
            // 城镇非住宅子表
            FwNoctiyHouseEntity noctiyHouseEntity = noCityTaskFromService.findNoCityById(id);
            noctiyHouseEntity.setSpType(Constant.SP_NOT);
            noctiyHouseEntity.setUpdateUser(user.getUserId());
            noctiyHouseEntity.setUpdateTime(new Date());
            noctiyHouseEntity.setBackOpinion(checkVo.getBackOpinion());
            noCityTaskFromService.update(noctiyHouseEntity);
        }
        if (Constant.BULIDING_VIllAGE.equals(bulidingType)) {
            // 农村住房子表
            FwVillageHouseEntity villageHouseEntity = villageHouseFormService.findVillageHouseById(id);
            villageHouseEntity.setSpType(Constant.SP_NOT);
            villageHouseEntity.setUpdateUser(user.getUserId());
            villageHouseEntity.setUpdateTime(new Date());
            villageHouseEntity.setBackOpinion(checkVo.getBackOpinion());
            villageHouseFormService.update(villageHouseEntity);
        }
        if (Constant.BULIDING_VIllAGEPUB.equals(bulidingType)) {
            // 农村公共建筑
            FwVillagePubhouseEntity villagePubhouseEntity = villagePubHouseFormService.findVillagePubHouseById(id);
            villagePubhouseEntity.setSpType(Constant.SP_NOT);
            villagePubhouseEntity.setUpdateUser(user.getUserId());
            villagePubhouseEntity.setUpdateTime(new Date());
            villagePubhouseEntity.setBackOpinion(checkVo.getBackOpinion());
            villagePubHouseFormService.update(villagePubhouseEntity);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void spFwCityForm(FwCtiyHouseEntity fwCtiyHouseEntity) {
        String userId = PlatformSessionUtils.getUserId();
        UserEntity user = PlatformAPI.getOrgAPI().getUserAPI().getUser(userId);
        String reason = fwCtiyHouseEntity.getReason();
        String status = fwCtiyHouseEntity.getSpType();
        String formId = fwCtiyHouseEntity.getFwId();
        taskFormRepository.spForm(status, reason, formId, new Date());
//        if (Constant.BULIDING_CITY.equals(spFormVO.getBuildingType())) {
//            cityTaskFormService.spReason(status, reason, formId);
//        }
//        if (Constant.BULIDING_NOCITY.equals(spFormVO.getBuildingType())) {
//            noCityTaskFromService.spReason(status, reason, formId);
//        }
//        if (Constant.BULIDING_VIllAGE.equals(spFormVO.getBuildingType())) {
//            villageHouseFormService.spReason(status, reason, formId);
//        }
//        if (Constant.BULIDING_VIllAGEPUB.equals(spFormVO.getBuildingType())) {
//            villagePubHouseFormService.spReason(status, reason, formId);
//        }
        FwCtiyHouseEntity descFwCtiyHouseEntity = cityTaskFormService.findCityById(fwCtiyHouseEntity.getFwId());
        fwCtiyHouseEntity =(FwCtiyHouseEntity) compareAndSwap(fwCtiyHouseEntity,descFwCtiyHouseEntity);
        fwCtiyHouseEntity.setUpdateTime(new Date());
        fwCtiyHouseEntity.setUpdateUser(userId);
        cityTaskFormService.update(fwCtiyHouseEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void spFwNoCityForm(FwNoctiyHouseEntity noctiyHouseEntity) {
        String userId = PlatformSessionUtils.getUserId();
        UserEntity user = PlatformAPI.getOrgAPI().getUserAPI().getUser(userId);
        String reason = noctiyHouseEntity.getReason();
        String status = noctiyHouseEntity.getSpType();
        String formId = noctiyHouseEntity.getFwId();
        taskFormRepository.spForm(status, reason, formId, new Date());
        FwNoctiyHouseEntity descFwNoCtiyHouseEntity = noCityTaskFromService.findNoCityById(noctiyHouseEntity.getFwId());
        noctiyHouseEntity = (FwNoctiyHouseEntity)compareAndSwap(noctiyHouseEntity,descFwNoCtiyHouseEntity);
        noctiyHouseEntity.setUpdateTime(new Date());
        noctiyHouseEntity.setUpdateUser(userId);
        noCityTaskFromService.update(noctiyHouseEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void spFwVillageForm(FwVillageHouseEntity villageHouseEntity) {
        String userId = PlatformSessionUtils.getUserId();
        UserEntity user = PlatformAPI.getOrgAPI().getUserAPI().getUser(userId);
        String reason = villageHouseEntity.getReason();
        String status = villageHouseEntity.getSpType();
        String formId = villageHouseEntity.getFwId();
        taskFormRepository.spForm(status, reason, formId, new Date());
        FwVillageHouseEntity descFwVillageHouseEntity = villageHouseFormService.findVillageHouseById(villageHouseEntity.getFwId());
        villageHouseEntity =(FwVillageHouseEntity) compareAndSwap(villageHouseEntity,descFwVillageHouseEntity);
        villageHouseEntity.setUpdateTime(new Date());
        villageHouseEntity.setUpdateUser(userId);
        villageHouseFormService.update(villageHouseEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void spFwVillagePubForm(FwVillagePubhouseEntity villagePubhouseEntity) {
        String userId = PlatformSessionUtils.getUserId();
        UserEntity user = PlatformAPI.getOrgAPI().getUserAPI().getUser(userId);
        String reason = villagePubhouseEntity.getReason();
        String status = villagePubhouseEntity.getSpType();
        String formId = villagePubhouseEntity.getFwId();
        taskFormRepository.spForm(status, reason, formId, new Date());
        FwVillagePubhouseEntity descFwVillagePubhouseEntity = villagePubHouseFormService.findVillagePubHouseById(villagePubhouseEntity.getFwId());
        villagePubhouseEntity =(FwVillagePubhouseEntity) compareAndSwap(villagePubhouseEntity,descFwVillagePubhouseEntity);
        villagePubhouseEntity.setUpdateTime(new Date());
        villagePubhouseEntity.setUpdateUser(userId);
        villagePubHouseFormService.update(villagePubhouseEntity);
    }

    @Transactional
    @Override
    public void batchBack(String ids, CheckVo checkVo) {
        String[] split = ids.split(",");
        List<String> idList = Arrays.asList(split);
        List<FwFormEntity> FwFormEntityList = taskFormRepository.findAllById(idList);
        String spType=Constant.SP_NOT;
        String backOpinion=checkVo.getBackOpinion();
        String userId = PlatformSessionUtils.getUserId();
        UserEntity user = PlatformAPI.getOrgAPI().getUserAPI().getUser(userId);
        //修改主表
        taskFormRepository.backAll(spType,backOpinion,idList);
        //修改子表
        for (FwFormEntity fwFormEntity : FwFormEntityList) {
            String formId = fwFormEntity.getFormId();
            String bulidingType = fwFormEntity.getBulidingType();
            if (Constant.BULIDING_CITY.equals(bulidingType)) {
                // 城镇住宅子表
                FwCtiyHouseEntity ctiyHouseEntity = cityTaskFormService.findCityById(formId);
                ctiyHouseEntity.setSpType(spType);
                ctiyHouseEntity.setUpdateUser(userId);
                ctiyHouseEntity.setUpdateTime(new Date());
                ctiyHouseEntity.setBackOpinion(backOpinion);
                cityTaskFormService.update(ctiyHouseEntity);
            }
            if (Constant.BULIDING_NOCITY.equals(bulidingType)) {
                //城镇非住宅子表
                FwNoctiyHouseEntity noctiyHouseEntity = noCityTaskFromService.findNoCityById(formId);
                noctiyHouseEntity.setSpType(spType);
                noctiyHouseEntity.setUpdateUser(userId);
                noctiyHouseEntity.setUpdateTime(new Date());
                noctiyHouseEntity.setBackOpinion(backOpinion);
                noCityTaskFromService.update(noctiyHouseEntity);
            }
            if (Constant.BULIDING_VIllAGE.equals(bulidingType)) {
                //农村住房
                FwVillageHouseEntity villageHouseEntity = villageHouseFormService.findVillageHouseById(formId);
                villageHouseEntity.setSpType(spType);
                villageHouseEntity.setUpdateUser(userId);
                villageHouseEntity.setUpdateTime(new Date());
                villageHouseEntity.setBackOpinion(backOpinion);
                villageHouseFormService.update(villageHouseEntity);
            }
            if (Constant.BULIDING_VIllAGEPUB.equals(bulidingType)) {
                //农村公共建筑
                FwVillagePubhouseEntity villagePubhouseEntity = villagePubHouseFormService.findVillagePubHouseById(formId);
                villagePubhouseEntity.setSpType(spType);
                villagePubhouseEntity.setUpdateUser(userId);
                villagePubhouseEntity.setUpdateTime(new Date());
                villagePubhouseEntity.setBackOpinion(backOpinion);
                villagePubHouseFormService.update(villagePubhouseEntity);
            }
        }
    }



    public Object compareAndSwap(Object obj1,Object obj2){
        if(obj1 instanceof  FwCtiyHouseEntity && obj2 instanceof  FwCtiyHouseEntity){
            FwCtiyHouseEntity sourceFwCtiyHouseEntity = (FwCtiyHouseEntity) obj1;
            FwCtiyHouseEntity descFwCtiyHouseEntity = (FwCtiyHouseEntity) obj2;
            Field[] fields = ReflectUtil.getFields(sourceFwCtiyHouseEntity.getClass());
            for (Field field : fields) {
                field.setAccessible(true);
                String name = field.getName();
                Object sourceValue = ReflectUtil.getFieldValue(sourceFwCtiyHouseEntity, field);
                Object descValue = ReflectUtil.getFieldValue(descFwCtiyHouseEntity, field);
                if(sourceValue==null && descValue!=null){
                    ReflectUtil.setFieldValue(sourceFwCtiyHouseEntity, field,descValue);
                }
            }
        }
        if(obj1 instanceof  FwNoctiyHouseEntity && obj2 instanceof  FwNoctiyHouseEntity){
            FwNoctiyHouseEntity sourceFwNoCtiyHouseEntity = (FwNoctiyHouseEntity) obj1;
            FwNoctiyHouseEntity descFwNoCtiyHouseEntity = (FwNoctiyHouseEntity) obj2;
            Field[] fields = ReflectUtil.getFields(sourceFwNoCtiyHouseEntity.getClass());
            for (Field field : fields) {
                field.setAccessible(true);
                String name = field.getName();
                Object sourceValue = ReflectUtil.getFieldValue(sourceFwNoCtiyHouseEntity, field);
                Object descValue = ReflectUtil.getFieldValue(descFwNoCtiyHouseEntity, field);
                if(sourceValue==null && descValue!=null){
                    ReflectUtil.setFieldValue(sourceFwNoCtiyHouseEntity, field,descValue);
                }
            }
        }
        if(obj1 instanceof  FwVillageHouseEntity && obj2 instanceof  FwVillageHouseEntity){
            FwVillageHouseEntity sourceFwVillageHouseEntity = (FwVillageHouseEntity) obj1;
            FwVillageHouseEntity descFwVillageHouseEntity = (FwVillageHouseEntity) obj2;
            Field[] fields = ReflectUtil.getFields(sourceFwVillageHouseEntity.getClass());
            for (Field field : fields) {
                field.setAccessible(true);
                String name = field.getName();
                Object sourceValue = ReflectUtil.getFieldValue(sourceFwVillageHouseEntity, field);
                Object descValue = ReflectUtil.getFieldValue(descFwVillageHouseEntity, field);
                if(sourceValue==null && descValue!=null){
                    ReflectUtil.setFieldValue(sourceFwVillageHouseEntity, field,descValue);
                }
            }
        }
        if(obj1 instanceof  FwVillagePubhouseEntity && obj2 instanceof  FwVillagePubhouseEntity){
            FwVillagePubhouseEntity sourceFwVillagePubhouseEntity = (FwVillagePubhouseEntity) obj1;
            FwVillagePubhouseEntity descFwVillagePubhouseEntity = (FwVillagePubhouseEntity) obj2;
            Field[] fields = ReflectUtil.getFields(sourceFwVillagePubhouseEntity.getClass());
            for (Field field : fields) {
                field.setAccessible(true);
                String name = field.getName();
                Object sourceValue = ReflectUtil.getFieldValue(sourceFwVillagePubhouseEntity, field);
                Object descValue = ReflectUtil.getFieldValue(descFwVillagePubhouseEntity, field);
                if(sourceValue==null && descValue!=null){
                    ReflectUtil.setFieldValue(sourceFwVillagePubhouseEntity, field,descValue);
                }
            }
        }
        return obj1;
    }
}
